package com.xjnt.base.support.admin.service;

import java.util.List;

import com.xjnt.base.support.admin.entity.Function;
import com.xjnt.base.support.admin.entity.Permission;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.frameworks.plugin.sql.SqlKit;
import com.xjnt.frameworks.plugin.sql.criterion.Restrictions;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;
import com.xjnt.frameworks.plugin.sql.criterion.expression.Order;

/**
 * @author caoyong
 *
 */
@Service
public class FunctionService extends BaseService<Function> {

	@Override
	public Function getModel() {
		return Function.me;
	}
	
	public List<Function> findByPermId(String permId){
		SqlCriteria criteria = new SqlCriteria(getModel(), "func");
		criteria.addRelation("sys_permission_function", "spf");
		criteria.addRelation(Permission.me, "perm");
		criteria.add(Restrictions.eqProp("func.uuid", "spf.functionid"));
		criteria.add(Restrictions.eqProp("perm.uuid", "spf.permissionid"));
		criteria.add(Restrictions.eq("perm.uuid", permId));
		criteria.addOrder(Order.asc("func.sorting"));
		return getModel().find(criteria.toSql(), permId);
	}
	
	public List<Function> findByPermIds(Object[] permIds){
		SqlCriteria criteria = new SqlCriteria(getModel(), "func");
		criteria.addRelation("sys_permission_function", "spf");
		criteria.addRelation(Permission.me, "perm");
		criteria.add(Restrictions.eqProp("func.uuid", "spf.functionid"));
		criteria.add(Restrictions.eqProp("perm.uuid", "spf.permissionid"));
		criteria.add(Restrictions.in("perm.uuid", permIds));
		criteria.addOrder(Order.asc("func.sorting"));
		return getModel().find(criteria.toSql(), permIds);
	}
	
	public List<Function> notAllocFunction(){
		return getModel().find(SqlKit.findSql("Function.notAllocFunction"));
	}
}
